<template>
	<view class="tui-addr-box">
		
		<form :report-submit="true">
			<tui-list-cell :hover="false" padding="0">
				<view class="tui-line-cell">
					<view class="tui-title">会员昵称</view>
					<view class="tui-title">{{myInfo.name}}</view>
				</view>
			</tui-list-cell>
			<tui-list-cell :hover="false" padding="0">
				<view class="tui-line-cell">
					<view class="tui-title">手机号码</view>
					<view class="tui-title">{{myInfo.phone}}</view>
				</view>
			</tui-list-cell>
			<tui-list-cell :hover="false" padding="0">
				<view class="tui-line-cell">
					<view class="tui-title">水票类型</view>
					<view class="tui-title" v-if="shipType == 'ship'">商家配送</view>
					<view class="tui-title" v-else>用户自提</view>
				</view>
			</tui-list-cell>
			<tui-list-cell :hover="false" padding="0">
				<view class="tui-line-cell">
					<view class="tui-title">剩余水票</view>
					<view class="tui-title" v-if="shipType == 'ship'">{{myTicket.ship_last_num}}</view>
					<view class="tui-title" v-else>{{myTicket.self_last_num}}</view>
				</view>
			</tui-list-cell>
			<tui-list-cell :hover="false" padding="0">
				<view class="tui-line-cell">
					<view class="tui-title">好友手机号</view>
					<input placeholder-class="tui-phcolor" class="tui-input" name="apply_phone" v-model="apply_phone" placeholder="好友的手机号码" maxlength="11" type="number" />
				</view>
			</tui-list-cell>
			<tui-list-cell :hover="false" padding="0">
				<view class="tui-line-cell">
					<view class="tui-title">赠送数目</view>
					<input placeholder-class="tui-phcolor" class="tui-input" name="apply_num" v-model="apply_num" placeholder="转移给好友的水票数目" maxlength="6" type="number" />
				</view>
			</tui-list-cell>
			<tui-list-cell :hover="false" padding="0">
				<view class="tui-line-cell">
					<view class="tui-title">申请说明</view>
					<input placeholder-class="tui-phcolor" class="tui-input" name="remark" v-model="remark" placeholder="赠送备注说明" maxlength="50" type="text" />
				</view>
			</tui-list-cell>
			
			<!-- 保存地址 -->
			<view class="tui-addr-save" @click="postToFriend()">
				<tui-button type="green" height="88rpx">信息无误 确认赠送</tui-button>
			</view>
			
			<view class="tui-protocol" hover-class="opcity" :hover-stay-time="150">
				<text class="tui-protocol-red">赠送不可逆~请认真核对账号信息~</text>
				<br><br>
				<text class="tui-protocol-red">赠送的数目为正整数</text>
				<br><br><br><br><br><br>
			</view>
		</form>
		
		<footer-menu :tabCur="2"></footer-menu>
	</view>
</template>

<script>
	import footerMenu from "@/components/footer-menu.vue"
	import tuiIcon from "@/components/thorui/tui-icon/tui-icon.vue"
	import tuiButton from "@/components/thorui/tui-button/tui-button"
	import tuiListView from "@/components/thorui/tui-list-view/tui-list-view"
	import tuiListCell from "@/components/thorui/tui-list-cell/tui-list-cell"
	const util = require('../../utils/util.js')
	export default {
		components: {
			footerMenu,
			tuiButton,
			tuiListView,
			tuiListCell,
			tuiIcon
		},
		data() {
			return {
				myName: '',
				myPhone: '',
				myMoneyGold: '',
				apply_phone: '',
				apply_num: '',
				remark: '',
				
				myInfo:{},
				myTicket:{},
				
				
				
				shipType: 'ship',
			}
		},
		methods: {
			
			
			
			//我的个人信息
			getMyBaseInfo: function() {
				uni.request({
					url: this.$apiUrl+ '/api/user/baseInfo',
					header: {
						'userId': uni.getStorageSync('userId'),
						'userToken': uni.getStorageSync('userToken'),
						'shopId': uni.getStorageSync('shopId')
					},
					success: (res) => {
						let resErr = res.data['error'];
						let resMsg = res.data['msg'];
						let resCode = res.data['code'];
						if(resCode == 401) {		//用户未登录
							uni.navigateTo({ url: '/pages/my/login' });
						}else if(resCode == 200){
							let resData = res.data['data'];
							this.myInfo = resData;
						}else{
							this.tui.toast(resMsg, 2000, false);
						}
					}
				});
			},
			
			
			//我的个人信息
			getMyTicketInfo: function() {
				uni.request({
					url: this.$apiUrl+ '/api/user/ticketInfo',
					header: {
						'userId': uni.getStorageSync('userId'),
						'userToken': uni.getStorageSync('userToken'),
						'shopId': uni.getStorageSync('shopId')
					},
					success: (res) => {
						let resErr = res.data['error'];
						let resData = res.data['data'];
						this.myTicket = resData;	
					}
				});
			},
			
			//消费积分赠送
			postToFriend: function() {
				if (util.isNullOrEmpty(this.apply_phone)) { this.tui.toast('请输入要赠送的好友的手机号'); return } 
				if (util.isNullOrEmpty(this.apply_num)) { this.tui.toast('请填写要转移的数目'); return } 
				if(this.shipType == 'ship') {
					if(Number(this.apply_num) > Number(this.myTicket.ship_last_num)) { this.tui.toast('你的当前水票不足'); return } 
				}
			
				if(this.shipType == 'self') {
					if(Number(this.apply_num) > Number(this.myTicket.self_last_num)) { this.tui.toast('你的当前水票不足'); return } 
				}
				
				if (util.isNullOrEmpty(this.remark)) { this.tui.toast('请输入申请说明'); return } 
				
				uni.request({
					url: this.$apiUrl + '/api/user/ticketToFriend',
					header: {
						'userId': uni.getStorageSync('userId'),
						'userToken': uni.getStorageSync('userToken'),
						'shopId': uni.getStorageSync('shopId'),
					},
					data: {
						apply_num: this.apply_num,
						apply_phone: this.apply_phone,
						remark: this.remark,
						type: this.shipType,
					},
					success: (res) => {
						let resMsg = res.data['msg'];
						let resErr = res.data['error'];
						if(resErr == 0 ){		//发送成功的时候
							this.tui.toast(resMsg);
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/shop/ticket-index'
								});
							}, 2000);
						}else{
							this.tui.toast(resMsg);
						}
					}
				});
			},
			
		},
		
		onLoad: function(options) {
			this.shipType = options.type || 'ship';
			
			this.getMyBaseInfo();
			this.getMyTicketInfo();
		},
		
	}
</script>

<style>
	
	

	.tui-addr-box {
		padding: 20rpx 0;
	}

	.tui-line-cell {
		width: 100%;
		padding: 24rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
	}

	.tui-title {
		width: 180rpx;
		font-size: 28rpx;
	}

	.tui-title-city-text {
		width: 180rpx;
		height: 40rpx;
		display: block;
		line-height: 46rpx;
	}

	.tui-input {
		width: 500rpx;
	}

	.tui-input-city {
		flex: 1;
		height: 40rpx;
		font-size: 28rpx;
		padding-right: 30rpx;
	}

	.tui-phcolor {
		color: #ccc;
		font-size: 28rpx;
	}
	.tui-cell-title{
		font-size: 28rpx;
	}
	.tui-addr-label {
		margin-left: 70rpx;
	}

	.tui-label-item {
		width: 76rpx;
		height: 40rpx;
		border: 1rpx solid rgb(136, 136, 136);
		border-radius: 6rpx;
		font-size: 26rpx;
		text-align: center;
		line-height: 40rpx;
		margin-right: 20rpx;
		display: inline-block;
		transform: scale(0.9);
	}
	.tui-label-active{
		background: #E41F19;
		border-color:#E41F19;
		color: #fff;
	}
	.tui-swipe-cell {
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background: #fff;
		padding: 10rpx 24rpx;
		border-radius: 6rpx;
		overflow: hidden;
		font-size: 28rpx;
	}
	
	.tui-addr-save {
		padding: 24rpx;
		margin-top: 100rpx;
	}
	
	
	.tui-protocol {
		color: #333;
		font-size: 24upx;
		text-align: center;
		width: 100%;
		margin-top: 29upx;
	}
	
	.tui-protocol-red {
		color: #f54f46;
	}
</style>